﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Peekaboo2008.dao;
using Peekaboo2008.model;

namespace Peekaboo2008.view
{
    public partial class FormSanPhamHangHoa : Form
    {
        String _MaHang = "";
        String _TenHang = "";
        public FormSanPhamHangHoa()
        {
            InitializeComponent();
            _MaHang = tstMahang.Text.Trim();
            _TenHang = tstTenHang.Text.Trim();
            FormSanPhamHangHoaLoad(_MaHang, _TenHang);
        }

        public void FormSanPhamHangHoaLoad(String maHang , String tenHang)
        {

            DataTable dt = HangHoaDAO.GetAllHangHoa(tstMahang.Text.Trim(), tstTenHang.Text.Trim());
            dgvSanPham.DataSource = dt;
        }

        private void dgvSanPham_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            FormSanPhamHangHoa formSanPham = this;
            HangHoaModel model = new HangHoaModel();
            model.ID = dgvSanPham.CurrentRow.Cells["Id"].Value.ToString();
            model.MaHang = dgvSanPham.CurrentRow.Cells["MaHang"].Value.ToString();
            model.Ten = dgvSanPham.CurrentRow.Cells["Ten"].Value.ToString();
            model.MaNhomHang = dgvSanPham.CurrentRow.Cells["maNhomHang"].Value.ToString();
            model.Thue = dgvSanPham.CurrentRow.Cells["Thue"].Value.ToString();
            model.ChietKhau = dgvSanPham.CurrentRow.Cells["ChietKhau"].Value.ToString();
            model.GhiChu = dgvSanPham.CurrentRow.Cells["GhiChu"].Value.ToString();
            model.DonVi = dgvSanPham.CurrentRow.Cells["donVi"].Value.ToString();
            model.GiaNhap = dgvSanPham.CurrentRow.Cells["giaNhap"].Value.ToString();
            model.GiaBanSi = dgvSanPham.CurrentRow.Cells["giaSi"].Value.ToString();
            model.GiaBanLe = dgvSanPham.CurrentRow.Cells["giaLe"].Value.ToString();
            model.TonDinhMuc = dgvSanPham.CurrentRow.Cells["tonDinhMuc"].Value.ToString();
            model.MoTa = dgvSanPham.CurrentRow.Cells["MoTa"].Value.ToString();
            model.BarCodeNew = dgvSanPham.CurrentRow.Cells["Barcode"].Value.ToString();
            FormChiTietSanPhamHangHoa formChiTietSanPham = new FormChiTietSanPhamHangHoa(model, ref formSanPham);
            formChiTietSanPham.ShowDialog();
        }

        private void FormSanPhamHangHoa_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormSanPhamHangHoa_KeyDown);
        }

        private void FormSanPhamHangHoa_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                tsbThem.PerformClick();
            }
            else if (e.KeyCode == Keys.F4)
            {
                tsbThoat.PerformClick();
            }
        }

        private void tsbThem_Click(object sender, EventArgs e)
        {
            FormSanPhamHangHoa formSanPham = this;
            FormChiTietSanPhamHangHoa formChiTietSanPham = new FormChiTietSanPhamHangHoa(ref formSanPham);
            formChiTietSanPham.ShowDialog();
        }

        private void tsbThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

      
      
        private void tstMahang_TextChanged(object sender, EventArgs e)
        {
            _MaHang = tstMahang.Text.Trim();
            _TenHang = tstTenHang.Text.Trim();
            FormSanPhamHangHoaLoad(_MaHang, _TenHang);
        }

        private void tstTenHang_TextChanged(object sender, EventArgs e)
        {
            _MaHang = tstMahang.Text.Trim();
            _TenHang = tstTenHang.Text.Trim();
            FormSanPhamHangHoaLoad(_MaHang, _TenHang);
        }
    }
}
